package Services

import(
	"github.com/gin-gonic/gin"
	"net/http"
	"gin/Common"
	"strconv"
	"fmt"
	"gin/Databases"
	"encoding/json"
)


// 宏观数据
func DisplayAll(c *gin.Context) {
	relation,_ := strconv.Atoi(c.Query("relation"))
	attr_name := c.Query("name")

	var relations []Common.RelationAll
	sql := "select Frelation_id,Fattribute_name,Fmatch_text from attribute_data where Frelation_id=? and Fattribute_name=? limit 2;"
	fmt.Println("sql:", sql)
	err := Databases.Db.Select(&relations, sql, relation, attr_name)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "query fail",
	        "data": "{}",
	    })
		fmt.Println("exec failed, ", err)
		return
	}

	if len(relations) < 1 {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "no data",
	        "data": "{}",
	    })
		fmt.Println("no data")
		return	
	}

	str, _ := json.Marshal(relations)
    str_res := string(str)

	c.JSON(http.StatusOK, gin.H{
        "result": 0,
        "msg":    "success",
        "data": str_res,
    })
}

